
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Teach,Ember-Route," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>查询参数 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        查询参数
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:37.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Route/" rel="tag">Ember-Route</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Teach/" rel="tag">Ember-Teach</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 6 分钟</span>
    </header>

    <div class="post-content">
      <p>查询参数是在URL的问号（？）右边部分，通常是键值对形式出现。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">http:&#x2F;&#x2F;example.com&#x2F;articles?sort&#x3D;ASC&amp;page&#x3D;2</span><br></pre></td></tr></table></figure>
<p>比如这个URL的查询参数有两个，一个是<code>sort</code>，一个是<code>page</code>，它们的值分别是<code>ASC</code>和<code>2</code>。</p>
<h3 id="1，指定查询参数"><a href="#1，指定查询参数" class="headerlink" title="1，指定查询参数"></a>1，指定查询参数</h3><p>查询参数通常是声明为<code>controller</code>类中。比如在当前活动路由<code>articles</code>下，你需要根据文章的类型<code>category</code>过滤，此时你必须要在<code>controller</code>内声明过滤参数<code>category</code>。</p>
<p>使用<a target="_blank" rel="noopener" href="http://ember-cli.com/user/guide">Ember CLI</a>新建一个<code>controller</code>、<code>route</code>：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ember g controller article；</span><br><span class="line">ember g route articles;</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/controllers/articles.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    queryParams: [<span class="string">&#x27;category&#x27;</span>],</span><br><span class="line">    category: <span class="literal">null</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>绑定一个查询参数到URL，并且参数的值为<code>null</code>。当你进入路由<code>articles</code>时，如果参数<code>category</code>的值发生变化会自动更新到<code>controller</code>中的<code>category</code>；反之亦然。你可以设置一个默认值，比如把<code>category</code>设置为<code>Java</code>。可以在模板上获取这个值。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/articles.hbs  --&gt;</span></span><br><span class="line">&#123;&#123;outlet&#125;&#125;</span><br><span class="line">category = &#123;&#123;category&#125;&#125;</span><br></pre></td></tr></table></figure>
<p>执行<a target="_blank" rel="noopener" href="http://localhost:4200/articles">http://localhost:4200/articles</a>，页面会显示出   <code>category = Java</code>。如果执行<a target="_blank" rel="noopener" href="http://localhost:4200/articles?category=PHP">http://localhost:4200/articles?category=PHP</a>，那么页面会显示<code>category = PHP</code>。</p>
<p>下面代码演示了怎么使用查询参数：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/controllers/articles.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    queryParams: [<span class="string">&#x27;category&#x27;</span>],</span><br><span class="line">    category: <span class="literal">null</span>,</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  定义一个返回数组的计算属性，可以直接在模板上遍历</span></span><br><span class="line">    filteredArticles: Ember.computed(<span class="string">&#x27;category&#x27;</span>, <span class="string">&#x27;model&#x27;</span>, <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">        <span class="keyword">var</span> category = <span class="built_in">this</span>.get(<span class="string">&#x27;category&#x27;</span>);</span><br><span class="line">        <span class="keyword">var</span> articles = <span class="built_in">this</span>.get(<span class="string">&#x27;model&#x27;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> (category) &#123;</span><br><span class="line">            <span class="keyword">return</span> articles.filterBy(<span class="string">&#x27;category&#x27;</span>, category);</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> articles;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;)</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>创建一个计算属性，这个计算属性是一个数组类型。由于是计算属性，并且这个计算属性关联了另外两个属性<code>category</code>和<code>model</code>，只要这两个属性其中之一发生改变都会导致<code>filteredArticles</code>发生改变，所以返回的数组元素也会跟着改变。</p>
<p>在<code>route</code>初始化测试数据。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/article.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    model(params) &#123;</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> [</span><br><span class="line">            &#123; <span class="attr">id</span>: <span class="number">1</span>, <span class="attr">title</span>: <span class="string">&#x27;Bower: dependencies and resolutions new&#x27;</span>, <span class="attr">body</span>: <span class="string">&quot;In the bower.json file, I see 2 keys dependencies and resolutionsWhy is that so? I understand Bower has a flat dependency structure. So has it got anything to do with that ?&quot;</span>, <span class="attr">category</span>: <span class="string">&#x27;java&#x27;</span> &#125;,</span><br><span class="line">            &#123; <span class="attr">id</span>: <span class="number">2</span>, <span class="attr">title</span>: <span class="string">&#x27;Highly Nested JSON Payload - hasMany error&#x27;</span>, <span class="attr">body</span>: <span class="string">&quot;Welcome to the Ember.js discussion forum. We&#x27;re running on the open source, Ember.js-powered Discourse forum software. They are also providing the hosting for us. Thanks guys! Please use this space for discussion abo⋯ read more&quot;</span>, <span class="attr">category</span>: <span class="string">&#x27;php&#x27;</span> &#125;,</span><br><span class="line">            &#123; <span class="attr">id</span>: <span class="number">3</span>, <span class="attr">title</span>: <span class="string">&#x27;Passing a jwt to my REST adapter new &#x27;</span>, <span class="attr">body</span>: <span class="string">&quot;This sets up a binding between the category query param in the URL, and the category property on controller:articles. In other words, once the articles route has been entered, any changes to the category query param in the URL will update the category property on controller:articles, and vice versa.&quot;</span>, <span class="attr">category</span>: <span class="string">&#x27;java&#x27;</span> &#125;</span><br><span class="line">        ];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>下面看看怎么在模板显示数据，并且根据<code>category</code>显示不同数据。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/articles.hbs  --&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-4 col-xs-4&quot;</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 输入的值会动态更新到controller  --&gt;</span></span><br><span class="line">    输入分类：&#123;&#123;input value=category placeholder =&#x27;查询的分类&#x27;&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 关键点：这里使用的是filteredArticles而不是从route获取的model --&gt;</span></span><br><span class="line">    &#123;&#123;#each filteredArticles as |item|&#125;&#125;</span><br><span class="line">        <span class="tag">&lt;<span class="name">li</span>&gt;</span></span><br><span class="line">            &#123;&#123;#link-to &#x27;articles.article&#x27; item&#125;&#125; &#123;&#123;item.title&#125;&#125;--&#123;&#123;item.category&#125;&#125; &#123;&#123;/link-to&#125;&#125;</span><br><span class="line">        <span class="tag">&lt;/<span class="name">li</span>&gt;</span>   </span><br><span class="line">    &#123;&#123;/each&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;col-md-8 col-xs-8&quot;</span>&gt;</span></span><br><span class="line">&#123;&#123;outlet&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>精彩的时刻到了！！先执行<a target="_blank" rel="noopener" href="http://localhost:4200/articles">http://localhost:4200/articles</a>，此时显示的是所有类型的数据。如下图：</p>
<p><img src="/content/images/2016/03/75.png" alt="result"></p>
<p>接着你就可以做点好玩的事了，直接在输入框输入分类名。由于计算属性的特性会自动更新数组<code>filteredArticles</code>。所以我们可以看到随着你输入字符的变化显示的数据也在变化！这个例子也说明了<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>计算属性自动更新变化的强大！！用着确实爽啊！！<br>官网教程没有说怎么在模板中使用，讲得也不是很明白，就给了一句</p>
<blockquote>
<p>Now we just need to define a computed property of our category-filtered array that the articles template will render:”<br>也有可能是我看不懂，反正摸索好一阵子才知道要这么用！！</p>
</blockquote>
<h3 id="2，使用link-to指定查询参数"><a href="#2，使用link-to指定查询参数" class="headerlink" title="2，使用link-to指定查询参数"></a>2，使用link-to指定查询参数</h3><p><code>link-to</code>助手使用<code>query-params</code>子表达式直接指定查询参数，需要注意的是这个表达式需要放在括号内使用，切记别少了这个括号。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!--  app/templates/articles.hbs  --&gt;</span></span><br><span class="line">⋯⋯</span><br><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    &#123;&#123;#link-to &#x27;articles&#x27; (query-params category=&#x27;java&#x27;)&#125;&#125; java &#123;&#123;/link-to&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    &#123;&#123;#link-to &#x27;articles&#x27; (query-params category=&#x27;php&#x27;)&#125;&#125; php &#123;&#123;/link-to&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    &#123;&#123;#link-to &#x27;articles&#x27; (query-params category=&#x27;&#x27;)&#125;&#125; all &#123;&#123;/link-to&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">⋯⋯</span><br></pre></td></tr></table></figure>
<p>在显示数据的ul标签后面新增上述两个<code>link-to</code>助手。它们的作用分别是指定分类类型为java、php、全部。但用户点击三个连接直接显示与连接指定类型匹配的数据（并且查询的输入框也变为链接指定的类型值）。比如我点击了第一个链接，输入显示如下图：</p>
<p><img src="/content/images/2016/03/76.png" alt="result"></p>
<h3 id="3，路由转换"><a href="#3，路由转换" class="headerlink" title="3，路由转换"></a>3，路由转换</h3><p><code>route</code>对象的<code>transitionTo</code>方法和<code>controller</code>对象的<code>transitionToRoute</code>方法都可以接受<code>final</code>类型的参数。并且这个参数是一个包括一个<code>key</code>为<code>queryParams</code>的对象。</p>
<p>修改前面已经创建好的路由<code>posts.js</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/posts.js</span></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    beforeModel: <span class="function"><span class="keyword">function</span>(<span class="params">params</span>) </span>&#123;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//  转到路由articles上，并且传递查询参数category，参数值为Java</span></span><br><span class="line">        <span class="built_in">this</span>.transitionTo(<span class="string">&#x27;articles&#x27;</span>, &#123; <span class="attr">queryParams</span>: &#123; <span class="attr">category</span>: <span class="string">&#x27;java&#x27;</span> &#125;&#125;);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>执行<a target="_blank" rel="noopener" href="http://localhost:4200/posts">http://localhost:4200/posts</a>后，可以看到路由直接跳转到<a target="_blank" rel="noopener" href="http://localhost:4200/articles?category=java">http://localhost:4200/articles?category=java</a>，实现了路由切换的同时也指定了查询的参数。界面显示的数据我就不截图了，程序不出错，显示的都是<code>category</code>为<code>java</code>的数据。</p>
<p>另外还有三种切换路由的方式。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  可以传递一个object过去</span></span><br><span class="line"><span class="built_in">this</span>.transitionTo(<span class="string">&#x27;articles&#x27;</span>, object, &#123; <span class="attr">queryParams</span>: &#123; <span class="attr">category</span>: <span class="string">&#x27;java&#x27;</span> &#125;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 这种方式不会改变路由，只是起到设置参数的作用，如果你在</span></span><br><span class="line"><span class="comment">//路由articles中使用这个方法，你的路由仍然是articles，只是查询参数变了。</span></span><br><span class="line"><span class="built_in">this</span>.transitionTo(&#123; <span class="attr">queryParams</span>: &#123; <span class="attr">direction</span>: <span class="string">&#x27;asc&#x27;</span> &#125;&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">//  直接指定跳转的URL和查询参数</span></span><br><span class="line"><span class="built_in">this</span>.transitionTo(<span class="string">&#x27;/posts/1?sort=date&amp;showDetails=true&#x27;</span>);</span><br></pre></td></tr></table></figure>
<p>上面的三种方式请读者自己编写例子试试吧。光看不练假把式⋯⋯</p>
<h3 id="4，选择进入一个完整的路由"><a href="#4，选择进入一个完整的路由" class="headerlink" title="4，选择进入一个完整的路由"></a>4，选择进入一个完整的路由</h3><p><code>transitionTo</code>和<code>link-to</code>提供的参数仅会改变查询参数的值，而不会改变路由的层次结构，这种路由的切换被认为是不完整的，这也就意味着比如<code>model</code>和<code>setupController</code>回调方法就不会被执行，只是使得<code>controller</code>里的属性值为新的查询参数值以及更新URL。</p>
<p>但是有些情况是查询参数改变需要从服务器重新加载数据，这种情况就需要一个完整的路由切换了。为了能在查询参数改变的时候切换到一个完整的路由你需要在<code>controller</code>对应的路由中配置一个名为<code>queryParams</code>哈希对象。并且需要设置一个名为<code>refreshModel</code>的查询参数，这个参数的值为<code>true</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">queryParams: &#123;</span><br><span class="line">    category: &#123;</span><br><span class="line">        refreshModel: <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;,</span><br><span class="line">model: <span class="function"><span class="keyword">function</span>(<span class="params">params</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="built_in">this</span>.store.query(<span class="string">&#x27;article&#x27;</span>, params);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>关于这段代码演示实例请查看官方提供的代码！</p>
<h3 id="5，使用replaceState更新URL"><a href="#5，使用replaceState更新URL" class="headerlink" title="5，使用replaceState更新URL"></a>5，使用replaceState更新URL</h3><p>默认情况下，<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>使用<code>pushState</code>更新URL来响应<code>controller</code>类中查询参数属性的变化，但是如果你想使用<code>replaceState</code>来替换<code>pushState</code>你可以在<code>route</code>类中的<code>queryParams</code>哈希对象中设置<code>replace</code>为<code>true</code>。设置为<code>true</code>表示启用这个设置。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">queryParams: &#123;</span><br><span class="line">category: &#123;</span><br><span class="line">    replaceState:<span class="literal">true</span></span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="6，将控制器的属性映射到不同的查询参数键值"><a href="#6，将控制器的属性映射到不同的查询参数键值" class="headerlink" title="6，将控制器的属性映射到不同的查询参数键值"></a>6，将控制器的属性映射到不同的查询参数键值</h3><p>默认情况下，在<code>controller</code>类中指定的查询属性<code>foo</code>会绑定到名为<code>foo</code>的查询参数上。比如：<code>?foo=123</code>。你也可以把查询属性映射到不同的查询参数上，语法如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/controllers/articles.js</span></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    queryParams: &#123;</span><br><span class="line">        category: <span class="string">&#x27;articles_category&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">    category: <span class="literal">null</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>这段代码就是把查询属性<code>category</code>映射到查询参数<code>articles_category</code>上。<br>对于有多个查询参数的情况你需要使用数组指定。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/controllers/articles.js</span></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    queryParams: [<span class="string">&#x27;page&#x27;</span>, <span class="string">&#x27;filter&#x27;</span>, &#123; <span class="attr">category</span>: <span class="string">&#x27;articles_category&#x27;</span> &#125;],</span><br><span class="line">    category: <span class="literal">null</span>,</span><br><span class="line">    page: <span class="number">1</span>,</span><br><span class="line">    filter: <span class="string">&#x27;recent&#x27;</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>上述代码定义了三个查询参数，如果需要把属性映射到不同名的参数需要手动指定，比如<code>category</code>。</p>
<h3 id="7，默认值与反序列化"><a href="#7，默认值与反序列化" class="headerlink" title="7，默认值与反序列化"></a>7，默认值与反序列化</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Controller.extend(&#123;</span><br><span class="line">    queryParams: <span class="string">&#x27;page&#x27;</span>,</span><br><span class="line">    page: <span class="number">1</span></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>在这段代码中设置了查询参数<code>page</code>的默认值为<code>1</code>。</p>
<p>这样的设置会有两种默认的行为：</p>
<p>1.查询的时候查询属性值会根据默认值的类型自动转换，所以当用户输入<a target="_blank" rel="noopener" href="http://localhost:4200/articles?page=1">http://localhost:4200/articles?page=1</a>的时候<code>page</code>的值<code>1</code>会被识别成数字<code>1</code>而不是字符<code>&#39;1&#39;</code>，应为设置的默认值<code>1</code>是数字类型。<br>2.当查询的值正好是默认值的时候，该值不会被序列化到URL中。比如查询值正好是<code>?page=1</code>这种情况URL可能是<code>/articles</code>，但是如果查询值是<code>?page=2</code>，URL肯定是<code>/articles?page=2</code>。</p>
<h3 id="8，粘性的查询参数值"><a href="#8，粘性的查询参数值" class="headerlink" title="8，粘性的查询参数值"></a>8，粘性的查询参数值</h3><p>默认情况下，在<a target="_blank" rel="noopener" href="http://emberjs.com/">Ember</a>中查询参数是“粘性的”，也就是说如果你改变了查询参数或者是离开页面又回退回来，新的查询值会默认在URL上，而不会自动清除（几乎所见的URL都差不多是这种情况）。这是个很有用的默认设置，特别是当你点击后退回到原页面的时候显示的数据没有改变。  </p>
<p>此外，粘性的查询参数值会被加载的<code>route</code>存储或者回复。比如，包括了动态段<code>/:post_id</code>的路由<code>posts</code>，以及路由对应的<code>controller</code>包含了查询属性<code>filter</code>。如果你导航到<code>/badgers</code>并且根据<code>reookies</code>过滤，然后再导航到<code>/bears</code>并根据<code>best</code>过滤，然后再导航到<code>/potatose</code>并根据<code>lamest</code>过滤。如下面的链接：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    &#123;&#123;#link-to &#x27;posts&#x27; &#x27;badgers&#x27;&#125;&#125;Badgers&#123;&#123;/link-to&#125;&#125;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    &#123;&#123;#link-to &#x27;posts&#x27; &#x27;bears&#x27;&#125;&#125;Bears&#123;&#123;/link-to&#125;&#125;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">    &#123;&#123;#link-to &#x27;posts&#x27; &#x27;potatoes&#x27;&#125;&#125;Potatoes&#123;&#123;/link-to&#125;&#125;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>模板编译之后得到如下HTML代码：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;/badgers?filter=rookies&quot;</span>&gt;</span>Badgers<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;/bears?filter=best&quot;</span>&gt;</span>Bears<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;/potatoes?filter=lamest&quot;</span>&gt;</span>Potatoes<span class="tag">&lt;/<span class="name">a</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>可以看到一旦你改变了查询参数，查询参数就会被存储或者是关联到<code>route</code>所加载的<code>model</code>上。如果你想重置查询参数你有如下两种方式处理：</p>
<p>1.在<code>link-to</code>或者<code>transitionTo</code>上显式指定查询参数的值；<br>2.使用<code>Route.resetController</code>回调设置查询参数的值并回退到切换之前的路由或者是改变<code>model</code>的路由。</p>
<p>下面的代码片段演示了一个查询参数在<code>controller</code>中重置为<code>1</code>，同时作用于切换前<code>ActiclesRoute</code>的<code>model</code>。结果就是当返回到当前路由时查询值已经被重置为<code>1</code>。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//  app/routes/article.js</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> Ember <span class="keyword">from</span> <span class="string">&#x27;ember&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> Ember.Route.extend(&#123;</span><br><span class="line"></span><br><span class="line">    resetController(controller, isExiting, transition) &#123;</span><br><span class="line">        <span class="comment">//  只有model发生变化的时候isExiting才为false</span></span><br><span class="line">        <span class="keyword">if</span> (isExiting) &#123;</span><br><span class="line">            <span class="comment">//  重置查询属性的值</span></span><br><span class="line">            controller.set(<span class="string">&#x27;page&#x27;</span>, <span class="number">1</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>某些情况下，你不想是用查询参数值限定路由模式，而是让查询参数值改变的时候路由也跟着改变并且会重新加载数据。这时候你可用在对应的<code>controller</code>类中设置<code>queryParams</code>哈希对象，在这对象中配置一个参数<code>scope</code>为<code>controller</code>。如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">queryParams: [&#123;</span><br><span class="line">    showMagnifyingGlass: &#123;</span><br><span class="line">        scope: <span class="string">&#x27;controller&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;]</span><br></pre></td></tr></table></figure>
<p>粘性的查询参数值这个只是点理解起来好难的说，看下一遍下来都不知道这个有何用！！！现在还是学习阶段还没真正在项目中使用这个特性，所以我也不知道怎么解释更容易理解，建议直接看官网教程吧！！</p>
<p><strong>说明</strong>：本文是基于官方2.0参考文档缩写，相对于其他版本内容会有出入。</p>
<p>以上的内容就是有关查询参数的全部了，主要是理解了查询参数的设置使用起来也就没什么问题。有点遗憾的是没能写出第4点的演示实例！能力有限只能遇到或者明白其使用的时候再补上了！！</p>
<br>
博文完整代码放在[Github](https://github.com/ubuntuvim/my_emberjs_code)（博文经过多次修改，博文上的代码与github代码可能又出入，不过影响不大！），如果你觉得博文对你有点用，请在github项目上给我点个`star`吧。您的肯定对我来说是最大的动力！！
    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/ember-teach/routing/%E6%A8%A1%E6%9D%BF%E6%B8%B2%E6%9F%93/" rel="next" title="模板渲染">
          模板渲染
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/ember-teach/routing/%E5%BC%82%E6%AD%A5%E8%B7%AF%E7%94%B1/" rel="prev" title="异步路由">
            异步路由
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%EF%BC%8C%E6%8C%87%E5%AE%9A%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0"><span class="toc-text">1，指定查询参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%EF%BC%8C%E4%BD%BF%E7%94%A8link-to%E6%8C%87%E5%AE%9A%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0"><span class="toc-text">2，使用link-to指定查询参数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%EF%BC%8C%E8%B7%AF%E7%94%B1%E8%BD%AC%E6%8D%A2"><span class="toc-text">3，路由转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%EF%BC%8C%E9%80%89%E6%8B%A9%E8%BF%9B%E5%85%A5%E4%B8%80%E4%B8%AA%E5%AE%8C%E6%95%B4%E7%9A%84%E8%B7%AF%E7%94%B1"><span class="toc-text">4，选择进入一个完整的路由</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5%EF%BC%8C%E4%BD%BF%E7%94%A8replaceState%E6%9B%B4%E6%96%B0URL"><span class="toc-text">5，使用replaceState更新URL</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6%EF%BC%8C%E5%B0%86%E6%8E%A7%E5%88%B6%E5%99%A8%E7%9A%84%E5%B1%9E%E6%80%A7%E6%98%A0%E5%B0%84%E5%88%B0%E4%B8%8D%E5%90%8C%E7%9A%84%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0%E9%94%AE%E5%80%BC"><span class="toc-text">6，将控制器的属性映射到不同的查询参数键值</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#7%EF%BC%8C%E9%BB%98%E8%AE%A4%E5%80%BC%E4%B8%8E%E5%8F%8D%E5%BA%8F%E5%88%97%E5%8C%96"><span class="toc-text">7，默认值与反序列化</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#8%EF%BC%8C%E7%B2%98%E6%80%A7%E7%9A%84%E6%9F%A5%E8%AF%A2%E5%8F%82%E6%95%B0%E5%80%BC"><span class="toc-text">8，粘性的查询参数值</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '查询参数', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
